#!/usr/bin/env bash
# Summary: Show the current PHP version
set -e
[ -n "$PHPENV_DEBUG" ] && set -x

if [ -z "$PHPENV_VERSION" ]; then
  PHPENV_VERSION_FILE="$(phpenv-version-file)"
  PHPENV_VERSION="$(phpenv-version-file-read "$PHPENV_VERSION_FILE" || true)"
fi

IFS=$'\n' read -d '' -r -a scripts <<<"$(phpenv-hooks version-name)" || true
for script in "${scripts[@]}"; do
  # shellcheck disable=SC1090
  source "$script"
done

if [ -z "$PHPENV_VERSION" ] || [ "$PHPENV_VERSION" = "system" ]; then
  echo "system"
  exit
fi

version_exists() {
  local version="$1"
  [ -d "${PHPENV_ROOT}/versions/${version}" ]
}

if version_exists "$PHPENV_VERSION"; then
  echo "$PHPENV_VERSION"
elif version_exists "${PHPENV_VERSION#php-}"; then
  echo "${PHPENV_VERSION#php-}"
else
  echo "phpenv: version \`$PHPENV_VERSION' is not installed (set by $(phpenv-version-origin))" >&2
  exit 1
fi
